package com._1.message.rabbitMq_order;

import org.springframework.amqp.core.*;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqOrderConfig {

        public  static  final String ORDER_QUEUE_NAME = "order_queue";
        public  static  final String ORDER_EXCHANGE_NAME = "order_exchange";
        public  static  final String ORDER_ROUTING_KEY = "order_routing_key";

        public static final String  DEAL_QUEUE_NAME = "deal_queue";
        public static final String  DEAL_EXCHANGE_NAME = "deal_exchange";
        public static final String  DEAL_ROUTING_KEY = "deal_routing_key";

        // 死信交换器
        @Bean
        public DirectExchange deadLetterExchange() {
            return new DirectExchange(DEAL_EXCHANGE_NAME);
        }

        @Bean
        public DirectExchange orderExchange() {
            return new DirectExchange(ORDER_EXCHANGE_NAME);
        }

    @Bean
    public Queue dealQueue() {
        return QueueBuilder.durable(DEAL_QUEUE_NAME)
                .build();
    }


    // 普通队列
    @Bean
    public Queue orderQueue() {
        return QueueBuilder.durable(ORDER_QUEUE_NAME)
                .withArgument("x-message-ttl",  20000)
                .withArgument("x-dead-letter-exchange", DEAL_EXCHANGE_NAME)
                .withArgument("x-dead-letter-routing-key", DEAL_ROUTING_KEY)
                .build();
    }

    // 绑定普通队列到普通交换器
    @Bean
    public Binding orderBinding(Queue orderQueue, DirectExchange orderExchange) {
        return BindingBuilder.bind(orderQueue).to(orderExchange).with(ORDER_ROUTING_KEY);
    }

    @Bean
    public Binding deadLetterBinding(Queue dealQueue, DirectExchange deadLetterExchange) {
        return BindingBuilder.bind(dealQueue).to(deadLetterExchange).with(DEAL_ROUTING_KEY);
    }


}
